const { observable, makeAutoObservable, action, computed } = require("mobx");

let id = 0
class AppleBasket {
    apples = []

    constructor() {
        makeAutoObservable(this,
            {
                apples: observable,
                addItem: action,
                eatApple: action,
                ateApples: computed,
                unateApples: computed,
            }
        )
    }
    //action
    addItem(weight) {
        const newItem = {
            id: id++,
            ate: false,
            weight
        }
        this.apples.push(newItem)
    }

    eatApple = (appleId) => {
        editItem(this.apples, ({ id }) => appleId === id, apple => apple.ate = true)
    }

    pickApple = () => {
        const weight = 180 + Math.floor(Math.random() * 50)
        this.addItem(weight)
    }

    //computed
    get ateApples() {
        return getFilterItems(this.apples, ({ ate }) => ate)
    }
    get unateApples() {
        return getFilterItems(this.apples, ({ ate }) => !ate)
    }
}

const store = new AppleBasket()
export default store

function getTotalWeight(items) {
    return items.reduce((total, { weight }) => total + weight, 0)
}

function getFilterItems(items, filter) {
    items = items.filter(filter)
    return { items, count: items.length, totoalWeight: getTotalWeight(items) }
}

function editItem(items, predicate, editItemFn) {
    items.forEach(item => predicate(item) && editItemFn(item))
}